package com.sky.Database;

import java.util.ArrayList;
import java.util.List;

import com.sky.Model.Office;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

/**
 * @author QuyenDT
 * 
 */
public class OfficeSQL {
	private SQLiteDatabase newDB;
	private List<Office> offcieList;

	public OfficeSQL(DBHelper dbHelper) {
		newDB = dbHelper.openDataBase();
	}

	public void retrieveOfficeList() {
		offcieList = new ArrayList<Office>();
		Cursor c = null;

		try {
			c = newDB.rawQuery("SELECT * FROM tbl_office_new " + " order by place", null);
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						Office office = new Office();
						office.setId(c.getInt(c.getColumnIndex("id")));
						//office.plate(c.getString(c.getColumnIndex("place")));
					
						office.setZipCode(c.getString(c.getColumnIndex("zip_code")));
						office.setName(c.getString(c.getColumnIndex("name")));
						office.setStreet(c.getString(c.getColumnIndex("street")));
						//office.setGender(c.getString(c.getColumnIndex("gender")));
//						office.setContact(c.getString(c.getColumnIndex("contact")));
//						office.setGender2(c.getString(c.getColumnIndex("gender2")));
//						office.setContact2(c.getString(c.getColumnIndex("contact2")));
						office.setTelephone(c.getString(c.getColumnIndex("telephone")));
						office.setTelephone2(c.getString(c.getColumnIndex("tel_link")));
						office.setFax(c.getString(c.getColumnIndex("fax")));
						office.setOpeningHours(c.getString(c.getColumnIndex("opening_time")));

						offcieList.add(office);
					} while (c.moveToNext());
				}
			}
		} catch (SQLiteException se) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			DBHelper.closeCursor(c);
		}
	}

	public Office getByID(long id) {
		Cursor c = null;

		try {
			c = newDB.rawQuery("SELECT * FROM tbl_office_new " + " WHERE id = " + id, null);
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						Office office = new Office();
						office.setId(c.getInt(c.getColumnIndex("id")));
//						office.setName(c.getString(c.getColumnIndex("place")));
						office.setZipCode(c.getString(c.getColumnIndex("zip_code")));
						office.setName(c.getString(c.getColumnIndex("name")));
						office.setStreet(c.getString(c.getColumnIndex("street")));
//						office.setGender(c.getString(c.getColumnIndex("gender")));
//						office.setContact(c.getString(c.getColumnIndex("contact")));
//						office.setGender2(c.getString(c.getColumnIndex("gender2")));
//						office.setContact2(c.getString(c.getColumnIndex("contact2")));
						office.setTelephone(c.getString(c.getColumnIndex("telephone")));
						office.setTelephone2(c.getString(c.getColumnIndex("tel_link")));
						office.setFax(c.getString(c.getColumnIndex("fax")));
						office.setOpeningHours(c.getString(c.getColumnIndex("opening_time")));
						return office;
					} while (c.moveToNext());
				}
			}
		} catch (SQLiteException se) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			DBHelper.closeCursor(c);
		}
		return null;
	}

	public List<Office> getOfficeList() {
		if (offcieList == null) {
			retrieveOfficeList();
		}
		return offcieList;
	}

}
